# 导入模块
import sys
# 窗口配置
from PyQt5.QtWidgets import *
# 图像相关
from PyQt5.QtGui import *
# QtCore相关
from PyQt5.QtCore import *
sys.path.append("../")
from ui.Ui_Furniture import Ui_HomeFunction
from common.hex_util import *

class MyHomeFunctionWidget(QWidget):
    
    sendAutoSignal = pyqtSignal(bytes)
    sendDoorSignal = pyqtSignal(bytes)
    sendDataSignal = pyqtSignal(bytes)
    
    def __init__(self, parent = None):
        super().__init__(parent)

        self.ui = Ui_HomeFunction()
        self.ui.setupUi(self)

        self.init_ui()

    def init_ui(self):
        self.ui.autoButton.clicked.connect(self.autoButtonSlot)
        self.ui.labourButton.clicked.connect(self.labourButtonSlot)
        self.ui.resetPasswordButton.clicked.connect(self.resetPasswordButtonSlot)
        self.ui.setPasswordButton.clicked.connect(self.setPasswordButtonSlot)
        self.ui.openCurtainButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.closeCurtainButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.openDoorButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.closeDoorButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.openWindowButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.closeWindowButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.openLightButton.clicked.connect(self.openOrCloseButtonSlot)
        self.ui.closeLightButton.clicked.connect(self.openOrCloseButtonSlot)
        
    def autoButtonSlot(self):
        verify = 0
        
        sendData = b'\xAA\x55\x10\x01'
        
        for i in range(len(sendData)):
            verify += sendData[i]           
        verify = verify & 0xFF
        sendData += bytes([verify % 256])
        
        self.sendAutoSignal.emit(sendData)
    
    def labourButtonSlot(self):
        verify = 0
        
        sendData = b'\xAA\x55\x10\x02'
        
        for i in range(len(sendData)):
            verify += sendData[i]           
        verify = verify & 0xFF
        sendData += bytes([verify % 256])
            
        self.sendAutoSignal.emit(sendData)
    
    def resetPasswordButtonSlot(self):
        verify = 0
        
        sendData = b'\xAA\x55\x01\x04'
        
        for i in range(len(sendData)):
            verify += sendData[i]           
        verify = verify & 0xFF
        sendData += bytes([verify % 256])
        
        self.sendDoorSignal.emit(sendData)
    
    def setPasswordButtonSlot(self):      
        newPassword = self.ui.passwordEdit.text()
        
        if len(newPassword) != 8:
            QMessageBox.information(self, '提示', '密码长度必须为8位')
            return
        
        verify = 0
        pwd = b''
        
        sendData = b'\xAA\x55\x01\x03'
        
        for i in range(len(newPassword)):
            pwd += bytes([int(newPassword[i]) % 256])
        
        sendData += pwd
        
        for i in range(len(sendData)):
            verify += sendData[i]
                  
        verify = verify & 0xFF
        print(bytes([verify % 256]))
        sendData += bytes([verify % 256])
        
        self.sendDoorSignal.emit(sendData)
    
    def openOrCloseButtonSlot(self):       
        verify = 0
        
        # 获取信号发出者
        sender = self.sender()
        
        if sender is self.ui.openLightButton:
            sendData = b'\xAA\x55\x04\x01'
        elif sender is self.ui.closeLightButton:
            sendData = b'\xAA\x55\x04\x02'
        elif sender is self.ui.openDoorButton:
            sendData = b'\xAA\x55\x01\x01'
        elif sender is self.ui.closeDoorButton:
            sendData = b'\xAA\x55\x01\x02'
        elif sender is self.ui.openWindowButton:
            sendData = b'\xAA\x55\x02\x01'
        elif sender is self.ui.closeWindowButton:
            sendData = b'\xAA\x55\x02\x02'
        elif sender is self.ui.openCurtainButton:
            sendData = b'\xAA\x55\x03\x01'
        elif sender is self.ui.closeCurtainButton:
            sendData = b'\xAA\x55\x03\x02'
        
        for i in range(len(sendData)):
            verify += sendData[i]
                  
        verify = verify & 0xFF
        sendData += bytes([verify % 256])

        self.sendDataSignal.emit(sendData)

if __name__ == '__main__':
    # 创建应用程序
    app = QApplication(sys.argv)

    # 创建窗口
    w = MyHomeFunctionWidget()

    # 显示窗口
    w.show()

    # 等待程序结束
    sys.exit(app.exec_())